package com.lan.iot;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.lan.iot.Activity_index.PREFS_NAME;

public class Signin extends AppCompatActivity {
    EditText et;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Window window = getWindow();

            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            window.setStatusBarColor(Color.TRANSPARENT);

            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.signin);
        context = getApplicationContext();
        et = findViewById(R.id.tel);
    }

    private boolean isPhoneNumberValid(String phoneNumber) {
        boolean isValid = false;
        /**
         * valid phone number format;
         */
        String expression1 = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
        /**
         * valid phone number format;
         */
        String expression2 = "^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
        CharSequence inputStr = phoneNumber;
        Pattern pattern1 = Pattern.compile(expression1);
        Matcher matcher1 = pattern1.matcher(inputStr);

        Pattern pattern2 = Pattern.compile(expression2);
        Matcher matcher2 = pattern2.matcher(inputStr);
        if (matcher1.matches() || matcher2.matches()) {
            isValid = true;
        }
        return isValid;
    }

    Context context;

    public void next(View view) {
        if (String.valueOf(et.getText()).length() == 11 && isPhoneNumberValid(String.valueOf(et.getText()))) {
            final SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("tel", String.valueOf(et.getText()));
            editor.commit();
            new Thread() {
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void run() {
                    Integer str = SendSmsUtf8.send(String.valueOf(et.getText()));

                    Intent intent = new Intent();
                    intent.setClass(Signin.this, setpwd.class);
                    intent.putExtra("str", str);
                    startActivity(intent);
                    finish();
                }
            }.start();
        } else {
            Toast.makeText(context, "不是可用的手机号", Toast.LENGTH_SHORT).show();

        }

//        Intent intent = new Intent();
//        intent.setClass(Signin.this,Signin.class);
//        startActivity(intent);
    }
}
